
	mui.init({});		
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;

	mui.plusReady(function() {
        var setButton = document.getElementById('do_post');
        var titleBox = document.getElementById('title');
		var contentBox = document.getElementById('content');

		setButton.addEventListener('tap', function() {
			var setInfo = {
				user_id: uid,
				title: titleBox.value,
				content: contentBox.value,
				_token: userinfo.token
			};
			var img_arr=new Array();
			
            if($('.fankui #img_list li').length>1){
            	$('.fankui #img_list li').each(function(index){
            		if($(this).find('img').length==1){
            			img_arr.push($(this).find('img').attr('src'));
            			//var base_img=uploadImg($(this).find('img').attr('src'));
            			//img_arr.push(base_img);
            		}
            	});
            	setInfo.img_list=img_arr;
            }

			if(setInfo.title.trim()=='') {
				plus.nativeUI.toast('请输入标题');
				return;
			}

			if(setInfo.content.trim()=='' || setInfo.content.trim().length<5) {
				plus.nativeUI.toast('请输入问题内容,至少为5个字');
				return;
			}
            if(setInfo.content.trim().length>200) {
				plus.nativeUI.toast('问题内容最多为200个字');
				return;
			}

            //console.log(JSON.stringify(setInfo));
			mui.ajax({
				url: apiurl + "Article/addBook/",
				data: setInfo,
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				beforeSend: function(data) {
                    plus.nativeUI.showWaiting(); 
                },
				success: function(res) {
					plus.nativeUI.closeWaiting(); 
					console.log(JSON.stringify(res));
					// 请求成功
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					} else {
						mui.toast(res.msg);
						mui.back();
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('提交失败！');
				}
			});
			
		}, false);
		
		
		
		mui('body').on('tap', '.mui-popover-action li>a', function() {
			var a = this,
				parent;
			//根据点击按钮，反推当前是哪个actionsheet
			for (parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
				if (parent.classList.contains('mui-popover-action')) {
					break;
				}
			}
			//关闭actionsheet
			mui('#' + parent.id).popover('toggle');
			if(a.getAttribute('id')=='xiangce'){
				//从相册选择
				galleryImg();
				return false;
			}
			if(a.getAttribute('id')=='paizao'){
				//拍照
				getImage();
				return false;
			}
		})
		
		//拍照
		function getImage() {
			var c = plus.camera.getCamera();
			c.captureImage(function(e) {
				plus.io.resolveLocalFileSystemURL(e, function(entry) {
					var s = entry.toLocalURL() + "?version=" + new Date().getTime();
					console.log(s);
					//document.getElementById("headimg").src = s;
					uploadImg(s);
					/*
					var htm='';
					htm+='<li>';
					htm+='<img src="'+s+'" width="80" height="80" />';
					htm+='<span class="mui-icon mui-icon-close" onclick="del_img(this)"></span>';
					htm+='</li>';
					$('.fankui #img_list #add_img').before(htm);
					if($('.fankui #img_list li').length==4){
					    $('.fankui #img_list #add_img').hide();
					}
					*/
					//变更大图预览的src
					//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
					document.querySelector("#__mui-imageview__group .mui-slider-item img").src = s + "?version=" + new Date().getTime();
				}, function(e) {
					console.log("读取拍照文件错误：" + e.message);
				});
			}, function(s) {
				console.log("error" + s);
			}, {
				filename: "_doc/head.jpg"
			})
		}
		
		
		//相册
		function galleryImg() {
			plus.gallery.pick(function(a) {
				plus.io.resolveLocalFileSystemURL(a, function(entry) {
					plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
						root.getFile("head.jpg", {}, function(file) {
							//文件已存在
							file.remove(function() {
								console.log("file remove success");
								entry.copyTo(root, 'head.jpg', function(e) {
										var e = e.fullPath + "?version=" + new Date().getTime();
										//document.getElementById("headimg").src = e;
										uploadImg(e);
										/*
										var htm='';
										htm+='<li>';
										htm+='<img src="'+e+'" width="80" height="80" />';
										htm+='<span class="mui-icon mui-icon-close" onclick="del_img(this)"></span>';
										htm+='</li>';
										$('.fankui #img_list #add_img').before(htm);
					                    if($('.fankui #img_list li').length==4){
					                    	$('.fankui #img_list #add_img').hide();
					                    }
					                    */
										//变更大图预览的src
										//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
										document.querySelector("#__mui-imageview__group .mui-slider-item img").src = e + "?version=" + new Date().getTime();;
									},
									function(e) {
										console.log('copy image fail:' + e.message);
									});
							}, function() {
								console.log("delete image fail:" + e.message);
							});
						}, function() {
							//文件不存在
							entry.copyTo(root, 'head.jpg', function(e) {
									var path = e.fullPath + "?version=" + new Date().getTime();
									document.getElementById("headimg").src = path;
									//document.getElementById("head-img1").src = path;
									//变更大图预览的src
									//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
									document.querySelector("#__mui-imageview__group .mui-slider-item img").src = path;
								},
								function(e) {
									console.log('copy image fail:' + e.message);
								});
						});
					}, function(e) {
						console.log("get _www folder fail");
					})
				}, function(e) {
					console.log("读取拍照文件错误：" + e.message);
				});
			}, function(a) {}, {
				filter: "image"
			})
		}


         //压缩图片   成64位编码
         function uploadImg11(path) {
			var img = new Image();
			img.src = path;
			//生成比例 
			var w = img.width,
				h = img.height,
				scale = w / h;
			
			//console.log(img.width);
		    //console.log(img.height);
			w = 480 || w; //480  你想压缩到多大
			h = w / scale;
		    //生成canvas
			var canvas = document.createElement('canvas');
			var ctx = canvas.getContext('2d');
			canvas.setAttribute('width', w);
			canvas.setAttribute('height', h);
			ctx.drawImage(img, 0, 0, w, h);
			var base64 = canvas.toDataURL('image/jpeg', 1 || 0.8); //1z 表示图片质量，越低越模糊。	 
			console.log(base64);
			base64 = base64.replace('data:image/jpeg;base64,', '');
			return base64;
		}
         
         
        function uploadImg(path) {
			var img = new Image();
			img.src = path;
			img.onload = function() {
				var that = this;
				//生成比例 
				var w = that.width,
					h = that.height,
					scale = w / h;
				w = 480 || w; //480  你想压缩到多大
				h = w / scale;
		
				//生成canvas
				var canvas = document.createElement('canvas');
				var ctx = canvas.getContext('2d');
				canvas.setAttribute('width', w);
				canvas.setAttribute('height', h);
				ctx.drawImage(that, 0, 0, w, h);
				var base64 = canvas.toDataURL('image/jpeg', 1 || 0.8); //1z 表示图片质量，越低越模糊。	 
				console.log(base64);
				//base64 = base64.replace('data:image/jpeg;base64,', '');
				mui.post(uploadurl, {
					file: base64,
					uid: uid,
					path : path,
					name : 'article'
				}, function(res) {
					console.log(JSON.stringify(res));
					if(res.status > 0) {
						//var img_path=serverurl + 'Uploads/trade/' + trade_id + '.jpg';
						var img_path = res.url;
						var htm='';
						htm+='<li>';
						htm+='<img src="'+img_path+'" width="80" height="80" />';
						htm+='<span class="mui-icon mui-icon-close" onclick="del_img(this)"></span>';
						htm+='</li>';
						$('.fankui #img_list #add_img').before(htm);
						if($('.fankui #img_list li').length==4){
						    $('.fankui #img_list #add_img').hide();
						}
						mui.toast('上传成功！');
					}
				},'json');
		
			}
		}
		

	});
	
	  //删除图片
		function del_img(obj){
			var liob=$(obj).parent('li');
			liob.remove();
			if($('.fankui #img_list li').length==4){
				$('.fankui #img_list #add_img').hide();
			}
			else{
				$('.fankui #img_list #add_img').show();
			}
		}
